package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostApiShenQingRuZhu;
import com.lc.afbx.util.MEditTextListener;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/5/23.
 */
public class ApplyJoinActivity extends BaseActivity implements View.OnClickListener {


    private EditText apply_join_introduce_et;
    private EditText apply_join_main_object_et;
    private EditText apply_join_phone_et;
    private EditText apply_join_connect_people_et;
    private EditText apply_join_address_et;
    private EditText apply_join_name_et;
    private ImageView left_iv;
    private ImageView right_iv;

    private int type = 1;

    private PostApiShenQingRuZhu postApiShenQingRuZhu = new PostApiShenQingRuZhu(new AsyCallBack<PostApiShenQingRuZhu.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiShenQingRuZhu.Info info) throws Exception {

            UtilToast.show(ApplyJoinActivity.this, "提交成功");
            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_shangjiaruzhu_shenqingruzhu);

        //要提交的属性
        apply_join_introduce_et = ((EditText) findViewById(R.id.apply_join_introduce_et));
        apply_join_main_object_et = ((EditText) findViewById(R.id.apply_join_main_object_et));
        apply_join_phone_et = ((EditText) findViewById(R.id.apply_join_phone_et));
        apply_join_connect_people_et = ((EditText) findViewById(R.id.apply_join_connect_people_et));
        apply_join_address_et = ((EditText) findViewById(R.id.apply_join_address_et));
        apply_join_name_et = ((EditText) findViewById(R.id.apply_join_name_et));

        // 点点 和点的布局
        left_iv = ((ImageView) findViewById(R.id.apply_join_left_iv));
        right_iv = ((ImageView) findViewById(R.id.apply_join_right_iv));

        // 进去默认选择  保险类商家

        //输入监听
        apply_join_introduce_et.addTextChangedListener(new MEditTextListener(context, 200, apply_join_introduce_et));
        apply_join_main_object_et.addTextChangedListener(new MEditTextListener(context, 30, apply_join_main_object_et));
        apply_join_phone_et.addTextChangedListener(new MEditTextListener(context, 11, apply_join_phone_et));
        apply_join_connect_people_et.addTextChangedListener(new MEditTextListener(context, 16, apply_join_connect_people_et));
        apply_join_address_et.addTextChangedListener(new MEditTextListener(context, 30, apply_join_address_et));
        apply_join_name_et.addTextChangedListener(new MEditTextListener(context, 16, apply_join_name_et));

        findViewById(R.id.apply_join_apply_process_tv).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.back:
                finish();
                return;

            case R.id.apply_join_insurance_business_ll:

                type = 1;

                left_iv.setImageResource(R.mipmap.nv);
                right_iv.setImageResource(R.mipmap.nan);

                return;

            case R.id.apply_join_not_insurance_business_ll:

                type = 2;

                left_iv.setImageResource(R.mipmap.nan);
                right_iv.setImageResource(R.mipmap.nv);

                return;

            case R.id.apply_join_submit_bt:


                String address_Str = apply_join_address_et.getText().toString().trim().replaceAll(" ", "");
                String people_Str = apply_join_connect_people_et.getText().toString().trim().replaceAll(" ", "");
                String phoneNum_Str = apply_join_phone_et.getText().toString().trim().replaceAll(" ", "");
                String zhuYingXiangMu_Str = apply_join_main_object_et.getText().toString().trim().replaceAll(" ", "");
                String intraduce_Str = apply_join_introduce_et.getText().toString().trim().replaceAll(" ", "");
                String name_Str = apply_join_name_et.getText().toString().trim().replaceAll(" ", "");

//                UtilLog.e("===============", "这里走没走啊" + name_Str.length() + (name_Str == null));

                if (TextUtils.isEmpty(name_Str)) {
                    UtilToast.show(this, "商家名称不能为空");
                    return;
                } else if (TextUtils.isEmpty(address_Str)) {
                    UtilToast.show(this, "商家地址不能为空");
                    return;
                } else if (TextUtils.isEmpty(people_Str)) {
                    UtilToast.show(this, "联系人不能为空");
                    return;
                } else if (TextUtils.isEmpty(phoneNum_Str)) {
                    UtilToast.show(this, "联系电话不能为空");
                    return;
                } else if (!Validator.isMobile(apply_join_phone_et.getText().toString())) {
                    //                判断手机号格式
                    UtilToast.show(this, "手机号格式错误");
                    return;
                } else if (TextUtils.isEmpty(zhuYingXiangMu_Str)) {
                    UtilToast.show(this, "主营项目不能为空");
                    return;
                } else if (TextUtils.isEmpty(intraduce_Str)) {
                    UtilToast.show(this, "商家简介不能为空");
                    return;
                }


                if (type == 1) {
                    postApiShenQingRuZhu.tid = PrefrenceConfig.ONE;
                } else {
                    postApiShenQingRuZhu.tid = PrefrenceConfig.TWO;
                }

                postApiShenQingRuZhu.userid = BaseApplication.BasePreferences.readUserId();
                postApiShenQingRuZhu.address = address_Str;
                postApiShenQingRuZhu.contact = people_Str;
                postApiShenQingRuZhu.tel = phoneNum_Str;
                postApiShenQingRuZhu.main = zhuYingXiangMu_Str;
                postApiShenQingRuZhu.content = intraduce_Str;
                postApiShenQingRuZhu.title = name_Str;

                postApiShenQingRuZhu.execute(this);


                return;

            case R.id.apply_join_apply_process_tv:

                startActivity(new Intent(this, ApplyJoinProcessActivity.class));

                break;

        }

    }


}
